#include "Demo_LineDetector.h"

#include <lines/impl/LineDetectorFactoryImpl.h>

#include <futil/im/io/UnifiedImageSequenceReader.h>

using namespace futil::im::io;

Demo_LineDetector::Demo_LineDetector(void)
{
}

Demo_LineDetector::~Demo_LineDetector(void)
{
}

void Demo_LineDetector::runLineDetectorOnImages(LineDetector* pLineDetector) {
	UnifiedImageSequenceReader reader(inputDirectory.c_str());
	IplImage* src = NULL;
	while ((src = reader.nextFrame()) != NULL) {
		process(pLineDetector, src, dst);
		saveLinesImage(dst, reader.getFilename());
	}
	if (dst != NULL) cvReleaseImage(&dst);
}

void Demo_LineDetector::setInputDirectory(const std::string& dir) {
	inputDirectory = dir;
}

void Demo_LineDetector::setOutputDirectory(const std::string& dir) {
	outputDirectory = dir;
}

void Demo_LineDetector::setUp() {
	pFactory = new LineDetectorFactoryImpl();
}

void Demo_LineDetector::tearDown() {
	delete pFactory;
}

void Demo_LineDetector::demo() {

	LineDetector* pLineDetector = pFactory->create("");
	
	setInputDirectory("");
	setOutputDirectory("");

	runLineDetectorOnImages(pLineDetector);
}

void Demo_LineDetector::process(LineDetector* pLineDetector, const IplImage* src, IplImage* dst) {
	vector<Line> lines = pLineDetector->detect(src);
	//linesDemoHelper
	linesDemoHelper.demoLines(src, lines);
}

void Demo_LineDetector::saveLinesImage(const IplImage* im, const char* filename) {
	string outputPath = outputDirectory + filename;
	cvSaveImage(outputPath.c_str(), im);
}

